// components/blog-ctrl/blog-ctrl.js
const db = wx.cloud.database();
import  fmt  from '../../utils/formatTime';
let userInfo = {}
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    blogId: String,
    blog:Object
  },
  externalClasses: [
    'iconfont', 'icon-pinglun', 'icon-fenxiang'
  ],

  /**
   * 组件的初始数据
   */
  data: {
    loginShow: false,
    modalShow: false,
    content: '',

  },

  /**
   * 组件的方法列表
   */
  methods: {
    onComment() {
      let that = this;
      wx.getSetting({
        success(res) {
          console.log(res.authSetting['scope.userInfo'], "res.authSetting['scope.userInfo']");
          if (res.authSetting['scope.userInfo']) {
            wx.getUserInfo({
              success(res) {
                console.log(res);
                userInfo = res.userInfo;
                // 显示评论弹出层
                that.setData({
                  modalShow: true
                })
              }
            })
          } else {
            that.setData({
              loginShow: true
            })
          }
        }
      })

    },

    onLoginSuccess(event) {
      userInfo = event.detail
      this.setData({
        loginShow: false
      }, () => {
        this.setData({
          modalShow: true
        })
      })
    },
    onLoginFail() {
      wx.showModal({
        title: "授权用户才能进行评论",
        content: ""
      })
    },

    onSubscribe() {
      let that = this;
      wx.requestSubscribeMessage({
        // 传入订阅消息的模板id，模板 id 可在小程序管理后台申请
        tmplIds: ['ESy95hWPUXPfoveViyiekUIMfbsXeZMNcSTcvij_YLM'],
        success(res) {
          // 申请订阅成功
          if (res.errMsg === 'requestSubscribeMessage:ok') {
            // 这里将订阅的课程信息调用云函数存入云开发数据
            // 推送模板信息
            wx.cloud.callFunction({
              name: "sendMessage",
              data: {
                content:that.data.content,
                nickName: userInfo.nickName,
                blogId: that.properties.blogId,
                createTime: fmt(new Date()),
                templateId:'ESy95hWPUXPfoveViyiekUIMfbsXeZMNcSTcvij_YLM'
              }
              // 注意：这个时间呢是需要转化格式才能够进行传递的不然会无法实现这个功能
            }).then((res) => {
              that.setData({
                content: "",
                modalShow: false
              })
              console.log(res,"this is err")
                wx.showToast({
                  title: '订阅成功',
                  icon: 'success',
                  duration: 2000,
                });
              })
              .catch((err) => {
                that.setData({
                  content: "",
                  modalShow: false
                })
                console.log(err,"this is err")
                wx.showToast({
                  title: '订阅失败',
                  icon: 'success',
                  duration: 2000,
                });
              });
          }
        },
      });
    },
    onSendInfo(event) {
      console.log(event);
      // 插入数据库
      let content = this.data.content;
      let formId = event.detail.formId;
      if (content.trim() == '') {
        wx.showModal({
          title: "评论内容不能为空",
          content: ""
        })
        return;
      }

      wx.showLoading({
        title: '评价中',
        mask: true
      })
      db.collection("blog-comment").add({
        data: {
          content,
          createTime: db.serverDate(),
          blogId: this.properties.blogId,
          nickName: userInfo.nickName,
          avatarUrl: userInfo.avatarUrl
        }
      }).then(res => {
        wx.hideLoading();
        wx.showToast({
          title: '评论成功',
        })
        this.triggerEvent("refreshCommentList")

        // 上线必改
        // this.setData({
        //   content: "",
        //   modalShow: false
        // })
        this.onSubscribe();
      })







    },
    onInput(event) {
      this.setData({
        content: event.detail.value
      })
    },

  }
})